package com.starsoft.smdc.crawler.sina;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.starsoft.smdc.bean.SmdcSecurity;
import com.starsoft.smdc.crawler.common.CrawlerService;
import com.starsoft.smdc.util.SecurityType;

public class SinaService extends CrawlerService {

	private static Logger logger = LoggerFactory.getLogger(SinaService.class);

	private final String SINA = "sina";

	protected String convertSecId(String secId, boolean isToLocal) {
		if (isToLocal) {
			List<SmdcSecurity> secs = securityDao.getByTicker(secId);
			if(secs.size()==0){
				throw new RuntimeException("Not find "+secId+" in DB");
			} else if(secs.size()>1){
				for(SmdcSecurity sec:secs ){
					if(!SecurityType.isIndex(sec.getSecType())){
						return sec.getSecId();
					}
				}
				throw new RuntimeException("Not find "+secId+" in DB");
			} else {
				return secs.get(0).getSecId();
			}
		} else {
			return secId.split(".")[0];
		}
	}

	protected Header[] defaultHeaders(String url) {
		List<Header> headers = new ArrayList<>();
		if(url.contains("hq.sinajs.cn")) {
			headers.add(new BasicHeader("Referer", "https://finance.sina.com.cn"));
		} else {
			
		}
		return headers.toArray(new Header[0]);
	}
	
	
	@Override
	protected String getTargetSystem() {
		return SINA;
	}

}
